<?php // $Id: index.php 12485 2007-05-27 04:43:35Z yannoo $
/*
==============================================================================
	Dokeos - elearning and course management software

	Copyright (c) 2004 Dokeos S.A.
	Copyright (c) 2003 Ghent University (UGent)
	Copyright (c) 2001 Universite catholique de Louvain (UCL)
	Copyright (c) Olivier Brouckaert

	For a full list of contributors, see "credits.txt".
	The full license can be read in "license.txt".

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	See the GNU General Public License for more details.

	Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
==============================================================================
*/
/**
==============================================================================
*	Index of the admin tools
*
*	@package dokeos.admin
==============================================================================
*/
// name of the language file that needs to be included <br />
$language_file=array('admin','tracking');

// resetting the course id
$cidReset=true;

// including some necessary dokeos files
require('../inc/global.inc.php');

require_once(api_get_path(LIBRARY_PATH).'security.lib.php');
include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');

// setting the section (for the tabs)
$this_section=SECTION_PLATFORM_ADMIN;

// Access restrictions
api_protect_admin_script();
$nameTools = get_lang("PlatformAdmin");

// setting breadcrumbs
$interbreadcrumb[] = array('url' => 'index.php', 'name' => $nameTools);

// setting the name of the tool
$tool_name="Create newsletter";

// Displaying the header
Display::display_header($tool_name);

/*
==============================================================================
		ACTION HANDLING
==============================================================================
*/
/*
if ($_POST['Register'])
{
	register_site();
	Display :: display_confirmation_message('Version Check Enabled');
}
*/
/*
==============================================================================
		MAIN SECTION
==============================================================================
*/
$keyword_url = Security::remove_XSS($_GET['keyword']);

?>

<div>
	<h4>Create newsletter</h4>
	<p>Please fill in the necessary details for your newsletter here. After your newsletter is created, you will be able to add contents.</p>
<?php
/*
if(isset($_POST['btnSubmit']))
{
	// Get data
	$title = trim($_POST['txtTitle']);
	if(!$title) $error = "<p>Title cannot be empty.</p>";
	$interval = strtolower($_POST['radInterval']);
	$deliveryDate = $_POST['datDelivery'];
	if($interval == "d")
		$deliveryTime = $_POST['timDelivery'];
	else
		$deliveryTime = "08:00";
	// Combines delivery date and time
	$delivery = "$deliveryDate $deliveryTime";
	
	// TODO: Security
	// Only allows interval to be "d", "m", or "w"
	$allowedInterval = array("d", "m", "w");
	// Defaults to monthly
	if(!in_array($interval, $allowedInterval)) $interval = "m";
	// Delivery time is valid?
	$dateCheck = date_create_from_format("Y-m-d H:i", $delivery);
	if($dateCheck === false)
		$error .= "<p>Date or time is not in the correct format.</p>";
	else
	{
		// Delivery time is in the past?
		$today = new DateTime("now");
		if($dateCheck < $today)
			$error .= "<p>You cannot send a newsletter in the past.</p>";
	}
	
	
	// Prepare statement
	if(!$error)
	{
		$link = api_mysqli_connect();
		$table = Database::get_main_table("newsletter");
		$query = "INSERT INTO $table(title, delivery_type, delivery_time, creator_id) VALUES(?, ?, ?, ?);";
		//echo $query;
		if($stmt = mysqli_prepare($link, $query))
		{
			global $_user;
			//echo "Prepared: $title, $interval, $delivery, $_user[user_id]";
			mysqli_stmt_bind_param($stmt, 'sssi', $title, $interval, $delivery, intval($_user['user_id']));
			if(mysqli_stmt_execute($stmt))
			{
				$newsletterId = mysqli_stmt_insert_id($stmt);
				Display::display_confirmation_message("<p>Your newsletter has been created. You can now <a href=\"newsletter_items.php?id=$newsletterId\">manage the news items</a>.</p>", false);
			}
			else
				Display::display_warning_message("<p>Your newsletter has not been created. Please try again later.</p>", false);
			mysqli_stmt_close($stmt);
			
			
		}
		mysqli_close($link);
	}
	else
		Display::display_error_message($error, false);
}
*/
// Create a form using FormValidator
$form = new FormValidator("new_newsletter");
$defaults['radInterval'] = 'd';
$form->add_textfield("txtTitle", "Title", true, array("maxlength" => "100", "placeholder" => "News Title", "autofocus" => ""));
$form->addElement('radio', 'radInterval', "Delivery interval", "Daily", "d");
$form->addElement('radio', 'radInterval', null, "Weekly", "w");
$form->addElement('radio', 'radInterval', null, "Monthly", "m");
/*
$radInterval[0] = $form->createElement("radio", null, null, "Daily", "d");
$radInterval[1] = $form->createElement("radio", null, null, "Weekly", "w");
$radInterval[2] = $form->createElement("radio", null, null, "Monthly", "m");
$form->addGroup($radInterval, "interval", "Delivery interval", "&nbsp;");
*/
$form->add_datepicker("datInterval", "Delivery time", true);
// Function to check delivery date is not in the past
function checkDeliveryTime($value)
{
	$time1 = mktime($value['H'],$value['i'],0,$value['F'],$value['d'],$value['Y']);
	$time2 = time();
	//echo "Time: $time1 $time2";
	return $time1 > $time2;
}
$form->addRule("datInterval", "You cannot deliver a newsletter in the past.", "callback", "checkDeliveryTime");
$form->add_html_editor("tarContent", "Content", true);
$form->addElement('static',null,null,"Tips: If you want to use HTML5, click the Source button and type your HTML5 there.<br />If you only see a plain box, just type your HTML5 tags there.");
$form->addElement('submit', null, "Create newsletter");
$form->setDefaults($defaults);
// Validate form
if($form->validate())
{
	// Get values
	$values = $form->exportValues();
	$title = $values['txtTitle'];
	$interval = $values['radInterval'];
	$delivery = $values['datInterval'];
	$content = $values['tarContent'];
	//echo $content;
	// TODO: Security
	// Only allows interval to be "d", "m", or "w"
	$allowedInterval = array("d", "m", "w");
	// Defaults to monthly
	if(!in_array($interval, $allowedInterval)) $interval = "m";
	global $_user;
	$link = api_mysqli_connect();
	$table = Database::get_main_table("newsletter");
	$query = "INSERT INTO $table(title, delivery_type, delivery_time, creator_id, content) VALUES(?, ?, ?, ?, ?);";
	if($stmt = mysqli_prepare($link, $query))
	{
		global $_user;
		//echo "Prepared: $title, $interval, $delivery, $_user[user_id]";
		mysqli_stmt_bind_param($stmt, 'sssis', $title, $interval, $delivery, intval($_user['user_id']), $content);
		if(mysqli_stmt_execute($stmt))
		{
			$newsletterId = mysqli_stmt_insert_id($stmt);
			Display::display_confirmation_message("<p>Your newsletter has been created.</p>", false);
		}
		else
			Display::display_warning_message("<p>Your newsletter has not been created. Please try again later.</p>", false);
		mysqli_stmt_close($stmt);
		
		
	}
	mysqli_close($link);
}
$form->display();
?>
	<!-- OLD WAY, no longer needed
	<form action="newsletter_create.php" method="post">
		<table>
			<tr>
				<td>Title</td>
				<td><input type="text" name="txtTitle" maxlength="100" <?php if(isset($_POST['txtTitle'])) echo "value=\"$_POST[txtTitle]\""; ?> /></td>
			</tr>
			<tr>
				<td>Delivery interval</td>
				<td>
					<input type="radio" name="radInterval" id="daily" value="d" onClick="toggleTimeOfDelivery();" />Daily<br />
					<input type="radio" name="radInterval" id="weekly" value="w" onClick="toggleTimeOfDelivery();" />Weekly<br />
					<input type="radio" name="radInterval" id="monthly" value="m" onClick="toggleTimeOfDelivery();" />Monthly
				</td>
			</tr>
			<tr>
				<td>Delivery time</td>
				<td>
					<input type="date" name="datDelivery" id="datDelivery" <?php if(isset($_POST['datDelivery'])) echo "value=\"$_POST[datDelivery]\""; ?> /> at <input type="time" name="timDelivery" id="timDelivery" <?php if(isset($_POST['timDelivery'])) echo "value=\"$_POST[timDelivery]\""; ?> disabled="disabled" />
					<br />
					Note that weekly and monthly newsletter will always be delivered at 08:00.<br />
					Please enter date in format <strong>yyyy-mm-dd</strong> and time in format <strong>HH:mm</strong> (24-hours).
				</td>
			</tr>
			<tr>
				<td>Content</td>
				<td><?php echo api_disp_html_area("tarContent", "", "250px"); ?></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" name="btnSubmit" id="btnSubmit" value="Create newsletter" /> <input type="reset" value="Reset" />
			</tr>
		</table>
	</form>
<script>
function toggleTimeOfDelivery() {
	document.getElementById("timDelivery").disabled = !(document.getElementById("daily").checked);
}
</script>
-->
</div>
<?php
/*
==============================================================================
		FOOTER
==============================================================================
*/
Display::display_footer();
?>
